package struts.action.departement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import dao.DAODepartement;

import struts.actionForm.BeanConnexionDep;

public class ActionConnexionDep extends Action {
	
	DAODepartement daoDep = null;
	
	public DAODepartement getDaoDep() {
		return daoDep;
	}

	public void setDaoDep(DAODepartement daoDep) {
		this.daoDep = daoDep;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		BeanConnexionDep bean = (BeanConnexionDep) form;
		
		HttpSession session = request.getSession();
		ActionErrors erreurs = new ActionErrors();
		
		try 
		{
			bean.setDepartement(getDaoDep().connexion(bean.getIdentifiant(), bean.getMdp()));
		} 
		catch (Exception e) {
			erreurs.add("Login et/ou mot de passe incorrects", new ActionMessage("beanConnexionDep.erreur.login.mdp.incorrects"));
			saveErrors(request, erreurs);
			//e.printStackTrace();
			return mapping.findForward("connexionDep");		
		}
		
		session.setAttribute("departement", bean.getDepartement());
		
		
		return mapping.findForward("accueilDep");
	}
	
	

}
